package test2;


import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;

/**
 * @author liule
 * @date 2023/8/7 14:37
 * @Desc :
 */
public class HttpClientTest2 {
    /**
     * 获取网络上的图片并保存到本地
     */
    public static void main(String[] args) throws Exception {
        //可关闭的httpclient客户端，相当于你打开的一个浏览器
        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
        String urlstr = "http://www.roadjava.com/thumbnail/2023/02/90301038497583594_mb.png";

        //构造httpGet请求对象
        HttpGet httpGet = new HttpGet(urlstr);
        // user-agent:解决HttpClient被认为不是真人行为
        httpGet.addHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36");

        // 可关闭的响应
        CloseableHttpResponse response = null;
        try {
            response = closeableHttpClient.execute(httpGet);
            // 代表本次请求的成功失败的状态
            StatusLine statusLine = response.getStatusLine();
            if (HttpStatus.SC_OK == statusLine.getStatusCode()) {
                HttpEntity entity = response.getEntity();
                // contentType 图片 ： image/png,image/jpg,image/图片的后缀...
                String contentType = entity.getContentType().getValue();
                String suffix = "-jpg";
                if (contentType.contains("jpg") || contentType.contains("jpeg")) {
                    suffix = ".jpg";
                } else if (contentType.contains("bmp") || contentType.contains("bitmap")) {
                    suffix = ".bmp";
                } else if (contentType.contains("png")) {
                    suffix = ".png";
                } else if (contentType.contains("gif")) {
                    suffix = ".gif";
                }
                // 获取文件的字节流
                byte[] bytes = EntityUtils.toByteArray(entity);
                // 本地图片下载路径
                String localAbsPath = "D:/javaWork/study/test20220719/src/main/resources/image/abc" + suffix;
                FileOutputStream fos = new FileOutputStream(localAbsPath);
                fos.write(bytes);
                fos.close();

                // 确保流关闭
                EntityUtils.consume(entity);
            } else {
                System.out.println("响应失败，响应码：" + statusLine.getStatusCode());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (closeableHttpClient != null) {
                closeableHttpClient.close();
            }
            if (response != null) {
                response.close();
            }
        }

    }
}
